/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.upload.service;

import com.google.common.collect.Lists;
import info.peigen.hotpot.common.core.logging.util.Base64ToUploadedFile;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceInvoke;
import info.peigen.hotpot.component.upload.service.order.Base64UploadBatchOrder;
import info.peigen.hotpot.component.upload.service.order.Base64UploadOrder;
import info.peigen.hotpot.component.upload.service.order.UploadBatchOrder;
import info.peigen.hotpot.component.upload.service.order.UploadOrder;
import info.peigen.hotpot.component.upload.service.result.UploadResult;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.handle.UploadedFile;

import java.util.List;

/**
 * <b>(UploadService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/11/14
 */
@Component
public class UploadService extends AbstractUploadService {

    public UploadResult upload(UploadOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new UploadResult()).build();

        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(activeUploadProcessor, uploadPathProcessor, uploadTransferFileProcessor);
            }
        });

        return (UploadResult) serviceContext.getResult();
    }

    public UploadResult uploadBatch(UploadBatchOrder order) {
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new UploadResult()).build();

        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(activeUploadBatchProcessor, uploadPathProcessor, uploadBatchTransferFileProcessor);
            }
        });

        return (UploadResult) serviceContext.getResult();
    }

    public UploadResult uploadBatch(Base64UploadBatchOrder batchOrder) {
        List<UploadedFile> uploadedFileList = Lists.newArrayList();
        for (String fileData : batchOrder.getFileData()) {
            UploadedFile uploadedFile = Base64ToUploadedFile.base64Convert(fileData);
            uploadedFileList.add(uploadedFile);
        }
        UploadBatchOrder order = UploadBatchOrder.builder()
                .uploadedFileList(uploadedFileList)
                .fileNames(batchOrder.getFileNames())
                .uploadPath(batchOrder.getUploadPath()).build();
        return uploadBatch(order);
    }

    public UploadResult upload(Base64UploadOrder base64) {
        UploadOrder order = UploadOrder.builder()
                .uploadedFile(Base64ToUploadedFile.base64Convert(base64.getFileData(), base64.getFileName()))
                .fileName(base64.getFileName())
                .uploadPath(base64.getUploadPath()).build();
        return upload(order);
    }

    public String findPath() {
        return uploadProperties.getFindPath();
    }
}